package com.imaker.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imaker.dao.BaseDao;
import com.imaker.domain.ModulesInfo;
import com.imaker.domain.Navigation;
import com.imaker.domain.Page;
import com.imaker.field.StaticField;
import com.imaker.utils.Validate;

 /**
 * <p>程序名称：       NavigationService.java</p>
 * <p>程序说明：       导航栏Service</p>
 * <p>版权信息：       Copyright mayonghua</p>
 * <p>时间：          Oct 25, 2010 10:49:23 AM</p>	
 * 
 * @author：          mayonghua
 * @version：         Ver 0.1
 */
@Service
public class NavigationService implements IService {
	
	private BaseDao<Navigation> baseDao ;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		   baseDao = new BaseDao<Navigation>(sessionFactory,Navigation.class);
	}

	/**
	 * 保存
	 * @param navigation
	 * @return
	 * @throws Exception
	 */
	public String save(Navigation navigation) throws Exception{
		if(Validate.entityNotNull(navigation))
		baseDao.save(navigation);
		return "success";
	}
	
	/**
	 * 更新
	 * @param navigation
	 * @return
	 * @throws Exception
	 */
	public String update(Navigation navigation) throws Exception {
		if(Validate.entityNotNull(navigation))
		baseDao.update(navigation);
		return "success";
	}
	
	/**
	 * 批量删除
	 * @param idList
	 * @return
	 * @throws Exception
	 */
	public String delete(List<String> idList) throws Exception {
		if(Validate.collectionNotNull(idList)){
			baseDao.deleteByIdList(idList);
		}
		return "success";
	}
	
	
	/**
	 * 根据ID进行查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Navigation findById(String id) throws Exception{
		 return baseDao.get(id);
	}
	
	/**
	 * 查询所有的导航栏，并按照设定的序号进行排序
	 * @return
	 * @throws Exception
	 */
	public List<Navigation> findAll() throws Exception{
		List<Navigation> list = baseDao.find(StaticField.NaviAllFindHql);
		return list;
	}
	
	/**
	 * 获得二级菜单
	 * @param nav
	 * @return
	 * @throws Exception
	 */
	public List<Navigation> listChildNav(Navigation nav) throws Exception{
		return baseDao.find(StaticField.ChildNaviFindHql,nav.getId());
	}
	
	/**
	 * 获得一级导航栏
	 */
	public List<Navigation> listRootNav() throws Exception{
		return baseDao.find(StaticField.NaviRootFindHql);
	}
	
	/**
	 * 查找Code为指定长度的导航栏条目
	 */
	public List<Navigation> findByCodeLen(String len) throws Exception {
		return baseDao.find(StaticField.NavCodeLenHql,len);
	}
	
	/**
	 * 安装展示的样子查询列表
	 * @return
	 */
	public List<Navigation> listForShow() throws Exception{
//		首先查询一级导航栏
		List<Navigation> rootNavList = listRootNav();
		for(Navigation nav : rootNavList){
//			查询二级导航栏
			List<Navigation> childNavList = listChildNav(nav);
			if(Validate.collectionNotNull(childNavList))
				nav.setChildNavList(childNavList);
		}
		return rootNavList;
	}
	
	/**
	 * 根据模块类型，分页查询内容信息
	 * @param page
	 * @param modulesTypeInfo
	 * @return
	 * @throws Exception
	 */
	public Page<Navigation> listByPage(Page page ,Navigation navigation) throws Exception {
		return baseDao.pagination(page,StaticField.NaivagtionByPage,"n.sortIndex", navigation.getId());
	}
	
}
